import {RemoteStorage} from './storage'
import logging from '../logging'

//skip upload if the local file is not newer than the remote oss object
export async function skipUpload(remoteStorage: RemoteStorage, objectKey: string, localModifiedTime: Date):Promise<boolean>{
    try{
        const result = await remoteStorage.head(objectKey);
        const headers = result.res.headers
        if (!('last-modified' in headers)){
            return Promise.resolve(false);
        }
        const remoteModifiedTime = new Date(headers['last-modified'] as string);
        return Promise.resolve(remoteModifiedTime.getTime() >= localModifiedTime.getTime())
    }catch(error:any){
        return Promise.resolve(false);
    }
}

//skip download if the oss object not newer than the local file
export async function skipDownload(remoteStorage: RemoteStorage, objectKey: string, localModifiedTime: Date):Promise<boolean>{
    try{
        const result = await remoteStorage.head(objectKey);
        const headers = result.res.headers
        if (!('last-modified' in headers)){
            return Promise.resolve(false);
        }
        const remoteModifiedTime = new Date(headers['last-modified'] as string);
        return Promise.resolve(remoteModifiedTime.getTime() <= localModifiedTime.getTime())
    }catch(error:any){
        return Promise.resolve(true);
    }
}
